﻿using Newtonsoft.Json.Linq;
using SAP.People.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SAP.People
{
    class InitTask : ITask
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private SpiderContext context;

        public InitTask(SpiderContext ctx)
        {
            context = ctx;
        }

        public void RunSynchronously()
        {
            context.Cookie = Settings.Default.Cookie;
            context.RootDir = "./res";
            context.InputFile = Path.Combine(context.RootDir, "chart.json");
            context.OutputFile = Path.Combine(context.RootDir, "chart.csv");
            context.IntermediateDir = Path.Combine(context.RootDir, "intermediate");

            if (!Directory.Exists(context.IntermediateDir))
            {
                Directory.CreateDirectory(context.IntermediateDir);
                log.InfoFormat("Create intermediate directory {0}", context.IntermediateDir);
            }

            LoadData();
            log.InfoFormat("Data loaded");
        }

        private void LoadData()
        {
            context.President = new Employee();

            dynamic root = JObject.Parse(File.ReadAllText(context.InputFile));
            JObject node = root.tree.node;
            Parse(context.President, null, node);

            GenerateFlatternData();
        }

        private void GenerateFlatternData()
        {
            context.Employees = new List<Employee>();
            LoadOneFlatternData(context.Employees, context.President);
        }

        private void LoadOneFlatternData(IList<Employee> list, Employee one)
        {
            list.Add(one);
            if (one.NumberOfChildren != 0)
            {
                foreach (var child in one.Subordinates)
                {
                    LoadOneFlatternData(list, child);
                }
            }
        }

        private void Parse(Employee employee, Employee manager, JObject node)
        {
            dynamic n = node;
            employee.NodeId = (int)n.nodeid;
            employee.UserId = (int)n.userid;
            employee.FirstName = (string)n.firstname;
            employee.LastName = (string)n.lastname;
            employee.NumberOfChildren = (int)n.numofchildren;

            employee.Manager = manager;

            if (employee.NumberOfChildren != 0)
            {
                employee.Subordinates = new List<Employee>(employee.NumberOfChildren);
                for (int i = 0; i < employee.NumberOfChildren; i++)
                {
                    Employee subordinate = new Employee();
                    JObject child = n.children[i];
                    Parse(subordinate, employee, child);
                    employee.Subordinates.Add(subordinate);
                }
            }
        }
    }
}
